<template>
	<view class="content fofa">
		<tabnav title="切换身份" :col="back"></tabnav>
		<view style="height: 40rpx;"></view>
		<view class="wid90 mar" v-for="(item,index) in datalist" v-show="item.is_show">
			<view class="hei60 dis disAl foSi30" style="color: #9D9C9C;">{{item.title}}</view>
			<!-- 选中状态 -->
			<view v-for="(item_s,index_s) in item.list" :key="index" style="height: 120rpx;"
				class="mar-top30 bacFFF box1 borRad20" :style="'border:1rpx solid'+back+';'" @click="switch_user(index,index_s)" v-if="item_s.id == insuse_userinfo.id">
				<view class="wid90 hei100 mar dis disAl disJuB">
					<view class="dis disAl">
						<image v-if="item_s.usertype > 1" :src="aurl+icon_list.qiye" style="width: 46rpx;height: 40rpx;"></image>
						<image v-else :src="aurl+icon_list.geren" style="width: 46rpx;height: 40rpx;"></image>
						<view class="mar-left20 foSi30">{{ item_s.accountname }}</view>
					</view>
					<view class="dis disAl">
						<image :src="aurl+icon_list.renzheng" style="width: 29rpx;height: 29rpx;"></image>
						<view class="mar-left20 foSi20">{{ item_s.accountauth == 1 ? '已认证' : '未认证' }}</view>
					</view>
				</view>
			</view>
			<!-- 未选中状态 -->
			<view v-for="(item_s,index_s) in item.list" :key="index" style="height: 120rpx;"
				class="mar-top30 bacFFF box1 borRad20" @click="switch_user(index,index_s)" v-else>
				<view class="wid90 hei100 mar dis disAl disJuB">
					<view class="dis disAl">
						<image v-if="item_s.usertype > 1" :src="aurl+icon_list.qiye" style="width: 46rpx;height: 40rpx;"></image>
						<image v-else :src="aurl+icon_list.geren" style="width: 46rpx;height: 40rpx;"></image>
						<view class="mar-left20 foSi30">{{ item_s.accountname }}</view>
					</view>
					<view class="dis disAl">
						<image :src="aurl+icon_list.renzheng" style="width: 29rpx;height: 29rpx;"></image>
						<view class="mar-left20 foSi20">{{ item_s.accountauth == 1 ? '已认证' : '未认证' }}</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- <view class="wid90 hei80 mar bacFFF borRad10 box dis disAl disJuC mar-top30" @click="tiaozhuan('/pages/wode/establishFirm')"
			:style="'color: '+back+';'" style="height: 100rpx;">+ 创建企业</view> -->
		<view style="height: 120rpx;"></view>
	</view>
</template>

<script>
	import tabnav from "../common/tabnav/tabnav.vue"
	export default {
		components: {
			tabnav,
		},
		data() {
			return {
				aurl:this.global.url,
				back: uni.getStorageSync('back'),
				userinfo:{},
				insuse_userinfo:{},
				token:'',
				datalist:[
					{'title':'个人身份',list:[],'is_show':false},
					{'title':'员工身份',list:[],'is_show':false},
					{'title':'企业身份',list:[],'is_show':false},
				],
				icon_list:{
					qiye:'/addon/qianyu_dzq/wechatimg/qiye.png',
					geren:'/addon/qianyu_dzq/wechatimg/geren.png',
					renzheng:'/addon/qianyu_dzq/wechatimg/renzheng.png',
				}
			}
		},
		onLoad() {
		},
		onShow() {
			let userinfo = uni.getStorageSync('main_userinfo')
			let insuse_userinfo = uni.getStorageSync('inuse_userinfo')
			if(userinfo){
				this.userinfo = userinfo
				this.token = uni.getStorageSync('main_userinfo_token')
				this.insuse_userinfo = insuse_userinfo
			}
			this.datalist[0]['list'] = []
			this.datalist[1]['list'] = []
			this.datalist[2]['list'] = []
			this.get_account()
		},
		methods: {
			get_account(){
				// 获取账号列表：这里需要使用主账号来获取，一个主账号对应多个子账号，不可反向查询(没做这功能)
				let that = this
				uni.showLoading({
					title:'加载中...'
				})
				uni.request({
					url: that.global.url+'api/qianyu_dzq/applet_user_accountlist',
					method: 'POST',
					header:{'site-id':that.global.site_id,'token':that.token},
					data: {},
					success(res) {
					    let result = res.data
						let cond = that.datalist
					    if(result.code == 1){
					       that.list = result.data
						   for(var i = 0;i<result.data.length;i++){
							   if(result.data[i]['usertype'] == 1){
								   cond[0]['list'].push(result.data[i])
								   cond[0]['is_show'] = true
							   }else if(result.data[i]['usertype'] == 2){
								   cond[2]['list'].push(result.data[i])
								   cond[2]['is_show'] = true
							   }else if(result.data[i]['usertype'] == 3){
								   cond[1]['list'].push(result.data[i])
								   cond[1]['is_show'] = true
							   }
						   }
					    }else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none'
					        })
					    }
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			tiaozhuan(url){
				uni.navigateTo({
					url: url
				});
			},
			switch_user(index,index2){
				let that = this
				let user = that.datalist[index]['list'][index2]
				uni.showLoading({
					title:'切换中...'
				})
				uni.request({
					url: that.global.url+'api/qianyu_dzq/applet_user_switch_account',
					method: 'POST',
					header:{'site-id':that.global.site_id,'token':that.token},
					data: {
						account_id:user.id
					},
					success(res) {
					    let result = res.data
						console.log(result)
					    if(result.code == 1){
					       uni.setStorageSync('inuse_userinfo_token', result.data) // 使用中的用户token
						   uni.setStorageSync('inuse_userinfo', user)   // 使用中的用户信息
						 
						   setTimeout(()=>{
							    uni.hideLoading()
								uni.navigateBack()
						   },1000)
					    }else{
							if(result.msg == '主账号无法重新获取token'){
								// 切换主账号
								uni.setStorageSync('inuse_userinfo', that.userinfo)   // 使用中的用户信息
								uni.setStorageSync('inuse_userinfo_token', that.token) // 使用中的用户token
								
								setTimeout(()=>{
									uni.hideLoading()
									uni.navigateBack()
								},1000)
								
							}else{
								uni.hideLoading()
								uni.showToast({
								    title: result.msg,
								    icon:'none'
								})
							}   
					    }
						uni.$emit('Refresh_userinfo',{msg:'更新用户信息重新获取数据'})
					},
					complete() {
						// uni.hideLoading()
					},
					fail() {
						uni.hideLoading()
					}
				})
			
			}
		}
	}
</script>

<style>
	.content {
		height: 100vh;
		background-color: #F4F3F3;
	}
</style>
